package com.appleword.biz.book.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.appleword.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.appleword.biz.book.domain.ApplewordArticle;
import com.appleword.biz.book.mapper.ApplewordBookMapper;
import com.appleword.biz.book.domain.ApplewordBook;
import com.appleword.biz.book.service.IApplewordBookService;

/**
 * 书Service业务层处理
 * 
 * @author apple
 * @date 2024-05-13
 */
@Service
public class ApplewordBookServiceImpl implements IApplewordBookService 
{
    @Autowired
    private ApplewordBookMapper applewordBookMapper;

    /**
     * 查询书
     * 
     * @param id 书主键
     * @return 书
     */
    @Override
    public ApplewordBook selectApplewordBookById(Long id)
    {
        return applewordBookMapper.selectApplewordBookById(id);
    }

    /**
     * 查询书列表
     * 
     * @param applewordBook 书
     * @return 书
     */
    @Override
    public List<ApplewordBook> selectApplewordBookList(ApplewordBook applewordBook)
    {
        return applewordBookMapper.selectApplewordBookList(applewordBook);
    }

    /**
     * 新增书
     * 
     * @param applewordBook 书
     * @return 结果
     */
    @Transactional
    @Override
    public int insertApplewordBook(ApplewordBook applewordBook)
    {
        int rows = applewordBookMapper.insertApplewordBook(applewordBook);
        insertApplewordArticle(applewordBook);
        return rows;
    }

    /**
     * 修改书
     * 
     * @param applewordBook 书
     * @return 结果
     */
    @Transactional
    @Override
    public int updateApplewordBook(ApplewordBook applewordBook)
    {
        applewordBookMapper.deleteApplewordArticleByBookId(applewordBook.getId());
        insertApplewordArticle(applewordBook);
        return applewordBookMapper.updateApplewordBook(applewordBook);
    }

    /**
     * 批量删除书
     * 
     * @param ids 需要删除的书主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteApplewordBookByIds(Long[] ids)
    {
        applewordBookMapper.deleteApplewordArticleByBookIds(ids);
        return applewordBookMapper.deleteApplewordBookByIds(ids);
    }

    /**
     * 删除书信息
     * 
     * @param id 书主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteApplewordBookById(Long id)
    {
        applewordBookMapper.deleteApplewordArticleByBookId(id);
        return applewordBookMapper.deleteApplewordBookById(id);
    }

    /**
     * 新增文章信息
     * 
     * @param applewordBook 书对象
     */
    public void insertApplewordArticle(ApplewordBook applewordBook)
    {
        List<ApplewordArticle> applewordArticleList = applewordBook.getApplewordArticleList();
        Long id = applewordBook.getId();
        if (StringUtils.isNotNull(applewordArticleList))
        {
            List<ApplewordArticle> list = new ArrayList<ApplewordArticle>();
            for (ApplewordArticle applewordArticle : applewordArticleList)
            {
                applewordArticle.setBookId(id);
                list.add(applewordArticle);
            }
            if (list.size() > 0)
            {
                applewordBookMapper.batchApplewordArticle(list);
            }
        }
    }
}